<?php

namespace Xin\ThinkPHP\Foundation\Bus;

use think\facade\Queue;

trait Dispatchable
{

    //	/**
    //	 * 默认队列
    //	 *
    //	 * @var string
    //	 */
    //	protected static $defaultQueue = '';

    /**
     * Dispatch the job with the given arguments.
     *
     * @param mixed $data
     * @param int $delay
     * @param string $queue
     * @return mixed
     */
    public static function dispatch($data = null, $delay = 0, $queue = '')
    {
        $queue = static::getQueue($queue);

        return Queue::later($delay, static::class, $data, $queue);
    }

    /**
     * Dispatch a command to its appropriate handler in the current process.
     *
     * @param mixed $data
     * @param string $queue
     * @return mixed
     */
    public static function dispatchNow($data = null, $queue = '')
    {
        $queue = static::getQueue($queue);

        return Queue::push(static::class, $data, $queue);
    }

    /**
     * 解析队列名称
     *
     * @param string $queue
     * @return string
     */
    private static function getQueue($queue)
    {
        if (empty($queue) && property_exists(static::class, 'defaultQueue')) {
            $queue = static::$defaultQueue;
        }

        return $queue;
    }

}
